var flashUtils=flashUtils || function(){//Avoid duplication contains errors that may affect
	var playState =0;
	var tobj;
	var obj;
	var mpwindw;
	var startFuncName='';
	var finishFuncName='';
	var goNexting=false;
	var currentTime=0;
	var createObj= function(){
			//obj=flashUtils.getFlashObj("musicUtils");
			if (typeof (obj)!== 'undefined'){
				clearInterval(tobj); 
				return true;
			}
			else{
				obj=flashUtils.getFlashObj("musicUtils");
				return false;
			}
		};
	var addEvent = (function () {
		if (document.addEventListener) {
			return function (el, type, fn) {
				if (el && el.nodeName || el === window) {
					el.addEventListener(type, fn, false);
				} else if (el && el.length) {
					for (var i = 0; i < el.length; i++) {
						addEvent(el[i], type, fn);
					}
				}
			};
		} else {
			return function (el, type, fn) {
				if (el && el.nodeName || el === window) {
					el.attachEvent('on' + type, function () { return fn.call(el, window.event); });
				} else if (el && el.length) {
					for (var i = 0; i < el.length; i++) {
						addEvent(el[i], type, fn);
					}
				}
			};
		}
	})();
	return {
		writeFlash:function(){
			//需要跟1象素播放器做互斥的播放器产品（互斥接口字符串，以","分割。）如MPW的接口为"_mpwLc"
			var flashArgs="_mpwLc";
			var musicStr="";
			musicStr+='<div id="musicPlayer" style="display:block; z-index:1024; padding:0px; margin:0px; width:1px; height:1px;top:0px; left:0px;overflow:hidden;position:fixed;*position:absolute;">';
			musicStr+='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="100%" height="100%" id="musicUtils" align="middle">';
			musicStr+='<param name="allowScriptAccess" value="always" />';
			musicStr+='<param name="wmode" value="window" />';
			musicStr+='<param name="movie" value="http://music.sina.com.cn/yueku/js/mwp/musicUtils.swf?version=20111010" />';
			musicStr+='<param name="quality" value="high" />';
			musicStr+='<param name="bgcolor" value="#FFFFFF" />';
			musicStr+='<param name="FlashVars" value="lcClient='+flashArgs+'" />';
			musicStr+='<embed src="http://music.sina.com.cn/yueku/js/mwp/musicUtils.swf?version=20111020"  FlashVars="lcClient='+flashArgs+'" quality="high" bgcolor="#FFFFFF"  height="100%" width="100%" wmode="window" name="musicUtils" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
			musicStr+='</object>';
			musicStr+='</div>';
			musicStr+='<audio id="musicBox" src=""></audio>';
			document.write(musicStr);
		},
		getFlashObj:function(flash_id){
			if (navigator.appName.indexOf("Microsoft") != -1) {
						if (window.document[flash_id])
						{
							return window.document[flash_id];
						}
						if (navigator.appName.indexOf("Microsoft Internet")==-1)
						{
							if (document.embeds && document.embeds[flash_id])
								return document.embeds[flash_id];
						}
						else 
						{
							return document.getElementById(flash_id);
						}
			}else {
				return document[flash_id];
			}
		},
		setPlayMode:function(num){
			obj.setPlayMode(num);
		},
		insertSong:function(ary,coFlag){
			if (this.checkIpad()){
				var str = ary[0];
				this.ipadPlay(str);
			}else{
				obj.insertSong(ary,coFlag);
			}
		},
		playSong:function(){
			if (this.checkIpad()){
				this.ipadPause();
			}else{
				obj.playSong();
			}
		},
		pauseSong:function(){
			if (this.checkIpad()){
				this.ipadPause();
			}else{
				obj.pauseSong();
			}
		},
		stopSong:function(){
			if (this.checkIpad()){
				this.ipadPause();
			}else{
				obj.stopSong();
			}
		},
		setVolume:function(num){
			if (this.checkIpad()){
				x = document.getElementById("musicBox");
				x.volume = num;
			}else
			{
				obj.setVolume(num);
			}
		},
		getVolume:function(){
			x = document.getElementById("musicBox");
			return x.volume;
		},
		seekSong:function(num){
			obj.seekSong(num);
		},
		addListChangeListener:function(str){
			obj.addListChangeListener(str);
		},
		delListChangeListener:function(){
			obj.delListChangeListener();
		},
		addStartListener:function(str){
			if (this.checkIpad()){
				flashUtils.startFuncName = str;
				return;
			}else{
				obj.addStartListener(str);
			}
		},
		delStartListener:function(){
			obj.delStartListener();
		},
		addProgressListener:function(str){
			obj.addProgressListener(str);
		},
		delProgressListener:function(){
			obj.delProgressListener();
		},
		addFinishListener:function(str){
			if (this.checkIpad()){
				flashUtils.finishFuncName = str;
			}else{
				obj.addFinishListener(str);
			}
		},
		delFinishListener:function(){
			obj.delFinishListener();
		},
		nextSong:function(){
			obj.nextSong();
		},
		preveSong:function(str){
			obj.preveSong();
		},
		getCurrentSong:function(){
			return obj.getCurrentSong();
		},
		getPlayList:function(){
			return obj.getPlayList();
		},
		setCookie:function(name,value,time){
			obj.setCookie(name,value,time);
		},
		getCookie:function(name){
			return obj.getCookie(name);
		},
		playSongsToMpw:function(ary,flag,coFlag){
			if (this.checkIpad()){
				var str='';
				var url ="http://music.sina.com.cn/yueku/ipad.php";
				var aryLength = ary.length;
				for (i=0;i<aryLength;i++){
					str += ary[i]+',';
				}
				str = str.substring(0,str.length-1);
				//url+=str;
				//window.open(url,"ipad");
				var tempHtml;
				 	tempHtml  ='<FORM id="tempform" method="get" target="ipad" action="http://music.sina.com.cn/t/ipad.php" style="display:none;">';
					tempHtml +='<input type="hidden" name="songids" value="'+str+'"/>';
					tempHtml +='<input type="hidden" name="flag" value="'+flag+'"/>';
					tempHtml +='<input type="hidden" name="coflag" value="'+coFlag+'"/>';
					tempHtml +='<input type="submit" value="提交"/>';
					tempHtml +='</form>';
				var oDiv=document.createElement("DIV");
				document.body.appendChild(oDiv);
				oDiv.innerHTML = tempHtml;
				var frm = document.getElementById("tempform");
				frm.submit();
				oDiv.removeChild(frm);
			}
			else{
				obj.playSongsToMpw(ary,flag,coFlag);
			}
			/*if(flashUtils.mpwindw){
				flashUtils.mpwindw.focus();
			}*/
		},
		playToWBMB:function(songidStr,songlistStr,flag,coFlag){
			if (this.checkIpad()){
				var url ="http://music.weibo.com/ting/play/";
				url += encodeURIComponent("songids="+songidStr+"|songlist="+songlistStr+"|flag="+flag+"|coflag="+coFlag);
				var tempHtml;
				 	tempHtml  ='<FORM id="tempform" method="post" target="ipad" action='+url+' style="display:none;">';
					//tempHtml +='<input type="hidden" name="songids" value="'+songidStr+'"/>';
					//tempHtml +='<input type="hidden" name="songlist" value="'+songlistStr+'"/>';
					//tempHtml +='<input type="hidden" name="flag" value="'+flag+'"/>';
					//tempHtml +='<input type="hidden" name="coflag" value="'+coFlag+'"/>';
					tempHtml +='<input type="submit" value="提交"/>';
					tempHtml +='</form>';
				var oDiv=document.createElement("DIV");
				document.body.appendChild(oDiv);
				oDiv.innerHTML = tempHtml;
				var frm = document.getElementById("tempform");
				frm.submit();
				oDiv.removeChild(frm);
			}
			else{
				obj.playToWBMB(songidStr,songlistStr,flag,coFlag);
			}
			/*if(flashUtils.WBMBwindow){
				flashUtils.WBMBwindow.focus();
			}*/			
		},
		playOutSongToWBMB:function(shortlink,flag,coFlag){
			obj.playOutSongToWBMB(shortlink,flag,flag);
		},
		playRadioToWBMB : function(link,sinaId,artistId,coFlag){
			var url = "http://music.weibo.com/ting/play/&sinaid="+sinaId+"&tab=firstpublish&coflag="+coFlag;
			if (this.checkIpad()){
				var tempHtml;
				 	tempHtml  ='<FORM id="tempform" method="post" target="ipad" action='+url+' style="display:none;">';
					tempHtml +='<input type="submit" value="提交"/>';
					tempHtml +='</form>';
				var oDiv=document.createElement("DIV");
				document.body.appendChild(oDiv);
				oDiv.innerHTML = tempHtml;
				var frm = document.getElementById("tempform");
				frm.submit();
				oDiv.removeChild(frm);
			}
			else{
				obj.playRadioToWBMB(url,sinaId,artistId,coFlag);
			}
			/*if(flashUtils.WBMBwindow){
				flashUtils.WBMBwindow.focus();
			}*/
		},
		playRecSongToMpw:function(songid,sinaId,recId,flag){
			obj.playRecSongToMpw(songid,sinaId,recId,flag);
			/*if(flashUtils.mpwindw){
				flashUtils.mpwindw.focus();
			}*/
		},
		playAlbumToMpw:function(str,flag,coFlag){
			obj.playAlbumToMpw(str,flag,coFlag);
			if(flashUtils.mpwindw){
				try{
					//flashUtils.mpwindw.focus();
				}catch(e){
					
				}
			}
		},
		//微博播放接口
		/**
		 *	url		mp3地址
		 *	sinaid
		 *	sinanick
		 *	content	微博内容  事例：刘德华-今天-<sina:link src="hibAP" name="hibAP" type="2"/>
		 */
		playWeiboSongsToMpw:function(url,sinaid,sinanick,content,flag){
			obj.playWeiboSongsToMpw(url,sinaid,sinanick,content,flag);
			if(flashUtils.mpwindw){
				try{
					//flashUtils.mpwindw.focus();
				}catch(e){
				
				}
			}
		},
		openMpw:function(str){
			var sw = screen.availWidth;
			var sh=screen.availHeight;
			var w=629,h=595;
			var wleft = (sw>w)?((sw-w)/2):0;
			var wtop = (sh>h)?((sh-h)/2):0;
			flashUtils.mpwindw = window.open(str,"w_yuekuplayer",'width=629,height=595,top='+wtop+',left='+wleft+',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');
			if(flashUtils.mpwindw){
				try{
					//flashUtils.mpwindw.focus();
				}catch(e){
				
				}
			}
		},
		openWBMB:function(str){
			var sw = screen.availWidth;
			var sh=screen.availHeight;
			var w=450,h=380;
			var wleft = (sw>w)?((sw-w)/2):0;
			var wtop = (sh>h)?((sh-h)/2):0;
			flashUtils.WBMBwindow = window.open(str,"w_wbmb"/*,'top='+wtop+',left='+wleft+',toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no'*/);
			if(flashUtils.WBMBwindow){
				try{
					//flashUtils.WBMBwindow.focus();
				}catch(e){
				
				}
			}
		},
		checkIpad:function(){
			var isiPad = navigator.userAgent.match(/iPad|iPhone|iPod/i) != null;
			return isiPad;
			//return true;
		},
		ipadPlay:function(songid){
			var url = "http://music.sina.com.cn/yueku/port/getmp3url.php?songid="+songid;
			x = document.getElementById("musicBox");
			Smc.Ajax.request('GET',url,function(data){
				x.setAttribute("src",data);
				x.load();
				x.play();
				if (flashUtils.startFuncName!=''){
					eval(flashUtils.startFuncName+"()");
				}
				playState = 1;
			});
		},
		ipadPause:function(){
			x = document.getElementById("musicBox");
			if (playState){
				x.pause();
				playState = 0;
			}else{
				x.play();
				playState = 1;
			}
			
		},
		setCurrentTime:function(){
				x = document.getElementById("musicBox");
				var duration = parseInt(x.duration);
				var currentTime = parseInt(x.currentTime);
				flashUtils.currentTime = currentTime;
				if ((currentTime >= duration-1)&&(currentTime>50)){

					if ((flashUtils.goNexting==false)||(typeof(flashUtils.goNexting)=="undefined")){
						if ((flashUtils.finishFuncName!='')&&(typeof(flashUtils.finishFuncName)!="undefined")){
							eval(flashUtils.finishFuncName+"()");
						}
						flashUtils.goNexting = true;
					}
				}
				if ((currentTime <10)&&(flashUtils.goNexting==true)){
					flashUtils.goNexting = false;
				}
		},
		getCurrentTime:function(){
			x = document.getElementById("musicBox");
			return x.currentTime;
		},
		goToTime:function(currentTime){
			x = document.getElementById("musicBox");
			x.currentTime = currentTime;
		},
		getBufferedTime:function(){
			x = document.getElementById("musicBox");
			return x.buffered.end().toString();
		},
		getTotalTime:function(){
			x = document.getElementById("musicBox");
			return x.duration;
		},
		init:function(){
			this.writeFlash();
			createObj();
			tobj = setInterval(createObj,10);
			if (this.checkIpad()){
				x = document.getElementById("musicBox");
					addEvent(x,'timeupdate',function() {
						flashUtils.setCurrentTime();
					});	
			}
		}
	};
}();

flashUtils.init();
